package com.lottery.service.pc28.accoutService.impl;

import com.lottery.dao.pc28.accoutDao.CustomerServiceDao;
import com.lottery.dto.PageDto;
import com.lottery.dto.ResultDto;
import com.lottery.dto.UtilsDto;
import com.lottery.entity.CustomerServiceInformation;
import com.lottery.service.pc28.accoutService.CustomerInformationService;
import com.lottery.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerInformationServiceimpl implements CustomerInformationService {

	@Autowired
	private CustomerServiceDao customerserviceDao;

	// 日志
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 添加客服
	 */
	@Override
	public ResultDto<CustomerServiceInformation> insertServer(CustomerServiceInformation customerserviceinformation) {
		ResultDto<CustomerServiceInformation> result = new ResultDto<CustomerServiceInformation>();
		logger.info("********customerserviceinformation********:" + customerserviceinformation);
		try {
			CustomerServiceInformation selectByAccout = customerserviceDao
					.selectByAccout(customerserviceinformation.getAccount());
			if (selectByAccout != null) {
				result.setSuccess(false);
				result.setMessage("用户已存在");
				return result;
			}
			// String str = "[\u4e00-\u9fa5]{1,7}";
			// String str1 = "[a-zA-Z]{1,14}";
			// String reg = "/^[0-9a-zA-Z]{3,14}$/";
			// if (customerserviceinformation.getAccount() != null
			// || !customerserviceinformation.getAccount().matches(reg)) {
			// result.setSuccess(false);
			// result.setMessage("账号只能输入英文和数字3到14位");
			// return result;
			// }

			if (customerserviceinformation.getServerName() != null
					&& !customerserviceinformation.getServerName().trim().equals("")
					&& StringUtil.strLength(customerserviceinformation.getServerName()) > 14) {
				result.setSuccess(false);
				result.setMessage("请输入不超过7个汉字或1-14个英文的姓名");
				return result;
			}

			if (customerserviceinformation.getNickName() != null
					&& !customerserviceinformation.getNickName().trim().equals("")
					&& StringUtil.strLength(customerserviceinformation.getNickName()) > 14) {
				result.setSuccess(false);
				result.setMessage("请输入不超过7个汉字或1-14个英文的昵称 ");
				return result;
			}
			// 对密码进行md5加密
			String password = UtilsDto.crypt(customerserviceinformation.getPassword());
			CustomerServiceInformation csi = new CustomerServiceInformation();
			csi.setAccount(customerserviceinformation.getAccount());
			csi.setServerName(customerserviceinformation.getServerName());
			csi.setNickName(customerserviceinformation.getNickName());
			csi.setPhoneNum(customerserviceinformation.getPhoneNum());
			csi.setPassword(password);
			csi.setServerType(customerserviceinformation.getServerType());
			csi.setMaximum(customerserviceinformation.getMaximum());
			customerserviceDao.insertByServer(csi);

			result.setData(csi);
			result.setSuccess(true);
			result.setMessage("添加成功");

		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("添加失败");
		}
		return result;
	}

	/**
	 * 显示客服列表
	 */
	@Override
	public ResultDto<PageDto> CustomerList(int pageNow, int serverType) {
		ResultDto<PageDto> resultDto = new ResultDto<PageDto>();
		PageDto pageDto = new PageDto();
		int pageSize = 10;
		int totalCount = 0;
		try {
			totalCount = customerserviceDao.selectTptalCount(serverType);
		} catch (Exception e) {
			resultDto.setSuccess(false);
			return resultDto;
		}
		if (serverType > 1) {
			resultDto.setSuccess(false);
			resultDto.setMessage("权限不足");
			return resultDto;
		}
		int totalPageCount = (totalCount + pageSize - 1) / pageSize;
		int startPage = (pageNow - 1) * pageSize;
		pageDto.setPageSize(pageSize);
		pageDto.setPageNow(pageNow);
		pageDto.setstartPage(startPage);
		pageDto.setTotalCount(totalCount);
		pageDto.setTotalPageCount(totalPageCount);
		pageDto.setType(serverType);
		resultDto.setData(pageDto);
		try {
			List<CustomerServiceInformation> selectByList = customerserviceDao.selectByList(pageDto);
			resultDto.setList(selectByList);
			resultDto.setSuccess(true);
		} catch (Exception e) {
			resultDto.setSuccess(false);
		}

		return resultDto;
	}

	/**
	 * 删除客服
	 */
	@Override
	public ResultDto<CustomerServiceInformation> deleteCustomer(int id) {
		ResultDto<CustomerServiceInformation> result = new ResultDto<CustomerServiceInformation>();
		try {
			customerserviceDao.deleteByServer(id);
			result.setSuccess(true);
			result.setMessage("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("删除失败");
		}
		return result;
	}

	/**
	 * 修改客服信息
	 */
	@Override
	public ResultDto<CustomerServiceInformation> updateServer(CustomerServiceInformation customerserviceinformation) {
		ResultDto<CustomerServiceInformation> result = new ResultDto<CustomerServiceInformation>();
		try {
			if (customerserviceinformation.getServerName() != null
					&& !customerserviceinformation.getServerName().trim().equals("")
					&& StringUtil.strLength(customerserviceinformation.getServerName()) > 14) {
				result.setSuccess(false);
				result.setMessage("请输入不超过7个汉字或1-14个英文的姓名");
				return result;
			}

			if (customerserviceinformation.getNickName() != null
					&& !customerserviceinformation.getNickName().trim().equals("")
					&& StringUtil.strLength(customerserviceinformation.getNickName()) > 14) {
				result.setSuccess(false);
				result.setMessage("请输入不超过7个汉字或1-14个英文的昵称 ");
				return result;
			}

			CustomerServiceInformation selectByAccout = customerserviceDao
					.selectByAccout(customerserviceinformation.getAccount());
			if (customerserviceinformation.getPassword().equals(selectByAccout.getPassword())) {
				customerserviceinformation.setAccount(customerserviceinformation.getAccount());
				customerserviceinformation.setServerName(customerserviceinformation.getServerName());
				customerserviceinformation.setNickName(customerserviceinformation.getNickName());
				customerserviceinformation.setPhoneNum(customerserviceinformation.getPhoneNum());
				customerserviceinformation.setServerType(customerserviceinformation.getServerType());
				customerserviceinformation.setPassword(selectByAccout.getPassword());
				customerserviceinformation.setMaximum(customerserviceinformation.getMaximum());
				customerserviceDao.updateByServerInformation(customerserviceinformation);
				result.setSuccess(true);
				result.setMessage("修改成功");
				return result;
			}

			if (customerserviceinformation.getPassword().length() == 32
					&& !customerserviceinformation.getPassword().equals(selectByAccout.getPassword())) {
				result.setSuccess(false);
				result.setMessage("密码超出了设置规则，请在10-20位数字和字母组合");
				return result;
			}
			// md5加密
			String password = UtilsDto.crypt(customerserviceinformation.getPassword());
			customerserviceinformation.setAccount(customerserviceinformation.getAccount());
			customerserviceinformation.setServerName(customerserviceinformation.getServerName());
			customerserviceinformation.setNickName(customerserviceinformation.getNickName());
			customerserviceinformation.setPhoneNum(customerserviceinformation.getPhoneNum());
			customerserviceinformation.setPassword(password);
			customerserviceinformation.setServerType(customerserviceinformation.getServerType());
			customerserviceinformation.setMaximum(customerserviceinformation.getMaximum());
			customerserviceDao.updateByServerInformation(customerserviceinformation);
			result.setSuccess(true);
			result.setMessage("修改成功");

		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("修改失败");
		}
		return result;
	}

	/**
	 * 根据客服id来查询出客服信息
	 */
	@Override
	public ResultDto<CustomerServiceInformation> findByIdServer(int id) {
		ResultDto<CustomerServiceInformation> result = new ResultDto<CustomerServiceInformation>();
		try {
			CustomerServiceInformation selectById = customerserviceDao.selectById(id);
			result.setData(selectById);
			result.setSuccess(true);
			result.setMessage("点击查询成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("点击查询失败");
		}
		return result;
	}

	@Override
	public ResultDto<CustomerServiceInformation> updateState(CustomerServiceInformation customerserviceinformation) {
		ResultDto<CustomerServiceInformation> result = new ResultDto<CustomerServiceInformation>();
		try {
			customerserviceDao.updateByState(customerserviceinformation);
			result.setSuccess(true);
			result.setMessage("200");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("400");
		}
		return result;
	}

	// /**
	// * 分页查询
	// */
	// @Override
	// public ResultDto<PageDto> selectByLimit(PageDto pageDto) {
	// ResultDto<PageDto> result = new ResultDto<PageDto>();
	// int startPage = pageDto.getstartPage();
	// int pageSize = pageDto.getPageSize();
	// List<CustomerServiceInformation> selectByList =
	// customerserviceDao.selectByList(startPage, pageSize);
	// int totalCount = customerserviceDao.selectTptalCount();
	// pageDto.setTotalCount(totalCount);
	// pageDto.setTotalPageCount(pageDto.getTotalPageCount());
	// result.setData(pageDto);
	// result.setList(selectByList);
	//
	// return result;
	// }

}
